﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace PhysicsSimulator
{
    enum ControlBirdState
    {
        IDLE,
        BEAT,
        PULLBACK
    }
    class CControlBird : CBird
    {
        CSprite _sticksSprite;        

        private const float MIN_ANGLE = (float)(Math.PI/6.0f);
        private const float MAX_ANGLE = -(float)(3.0f*Math.PI/5.0f);

        float _sticksAngle = -(float)(3.0f * Math.PI / 5.0f);

        ControlBirdState _currentBirdState = ControlBirdState.IDLE;
        public ControlBirdState CurrentBirdState
        {
            get
            {
                return _currentBirdState;
            }
            set
            {
                _currentBirdState = value;
            }
        }
        

        public CControlBird(SpriteBatch spriteBatch)
            : base(spriteBatch)
        {

        }

        public override void Init(ContentManager contentManager)
        {
            base.Init(contentManager);
            _sticksSprite = new CSprite(contentManager.Load<Texture2D>("Images/CandyGame/sticks"));
            _sticksSprite.Anchor = new Vector2(0.5f,1.075f);
        }

        public override void Update(GameTimerEventArgs timer)
        {
            base.Update(timer);
            switch (_currentBirdState)
            {
                case ControlBirdState.IDLE:
                    _sticksAngle = MIN_ANGLE;
                    break;
                case ControlBirdState.BEAT:
                    _sticksAngle -= (0.15f - (_sticksAngle - MIN_ANGLE)*0.25f);
                    if (_sticksAngle < MAX_ANGLE)
                    {
                        _sticksAngle = MAX_ANGLE;
                        _currentBirdState = ControlBirdState.PULLBACK;
                        AudioManager.PlaySound("CandyHit");
                    }
                    
                    break;
                case ControlBirdState.PULLBACK:
                    _sticksAngle += 0.1f;
                    if (_sticksAngle > MIN_ANGLE)
                    {
                        _sticksAngle = MIN_ANGLE;
                        _currentBirdState = ControlBirdState.IDLE;
                    }                                  
                    break;
            }
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            SpriteBatch.Draw(_sticksSprite.Texture,BirdPosition + new Vector2(-35,15),null,Color.White,_sticksAngle,_sticksSprite.Origin,1.0f,SpriteEffects.None,0);
            base.Draw(timer);
        }

        public bool Beat()
        {
            if (_currentBirdState == ControlBirdState.IDLE || _currentBirdState == ControlBirdState.PULLBACK)
            {
                _currentBirdState = ControlBirdState.BEAT;
                return true;
            }
            return false;
        }

        public Vector2 GetSticksPoint()
        {
            Vector2 OR = Vector2.Subtract(new Vector2(BirdPosition.X, BirdPosition.Y - _sticksSprite.Height), BirdPosition);
            Vector2 OP = RotateByAngle(OR, _sticksAngle);
            Vector2 P = Vector2.Add(BirdPosition, OP);
            return P;
        }

        private Vector2 RotateByAngle(Vector2 v, float angle)
        {
            Vector2 r = Vector2.Zero;
            r.X = (float)(v.X * Math.Cos(angle) - v.Y * Math.Sin(angle));
            r.Y = (float)(v.X * Math.Sin(angle) + v.Y * Math.Cos(angle));
            return r;
        }

        public float GetForce()
        {
            return Math.Abs(_sticksAngle - MIN_ANGLE);
        }
    }
}
